package com.ruoyi.project.system.info.controller;

import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.info.domain.JobInfo;
import com.ruoyi.project.system.info.service.IJobInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 查询作业信息Controller
 * 
 * @author ruoyi
 * @date 2021-12-26
 */
@Controller
@RequestMapping("/jobinfo/info")
public class JobInfoController extends BaseController {
    private String prefix = "jobinfo/info";

    @Autowired
    private IJobInfoService jobInfoService;

    @RequiresPermissions("jobinfo:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 查询查询作业信息列表
     */
    @RequiresPermissions("jobinfo:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JobInfo jobInfo) {   //获得当前用户登录ID
        Long userId = ShiroUtils.getUserId();
        //获得当前用户部门ID
        Long deptId = ShiroUtils.getSysUser().getDeptId();
        //获取用户对象
        User sysuser = ShiroUtils.getSysUser();
        List<Role> roles = sysuser.getRoles();
        Role sysRole = roles.get(0);
        //教师角色ID=3
        if (sysRole.getRoleId() == 3) {
            //如果是老师角色只显示当前老师操作的数据
            jobInfo.setTeacherId(String.valueOf(userId));
        }
        //学生角色id是4
        else if (sysRole.getRoleId() == 4) {
            //如果是学生角色只显示当前学生操作的数据
            jobInfo.setStuclassid(deptId);
        }


        startPage();
        List<JobInfo> list = jobInfoService.selectJobInfoList(jobInfo);
        return getDataTable(list);
    }

    /**
     * 导出查询作业信息列表
     */
    @RequiresPermissions("jobinfo:info:export")
    @Log(title = "查询作业信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JobInfo jobInfo) {
        List<JobInfo> list = jobInfoService.selectJobInfoList(jobInfo);
        ExcelUtil<JobInfo> util = new ExcelUtil<JobInfo>(JobInfo.class);
        return util.exportExcel(list, "查询作业信息数据");
    }

    /**
     * 新增查询作业信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存查询作业信息
     */
    @RequiresPermissions("jobinfo:info:add")
    @Log(title = "查询作业信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JobInfo jobInfo) {
        return toAjax(jobInfoService.insertJobInfo(jobInfo));
    }

    /**
     * 修改查询作业信息
     */
    @RequiresPermissions("jobinfo:info:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        JobInfo jobInfo = jobInfoService.selectJobInfoById(id);
        mmap.put("jobInfo", jobInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存查询作业信息
     */
    @RequiresPermissions("jobinfo:info:edit")
    @Log(title = "查询作业信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JobInfo jobInfo) {
        return toAjax(jobInfoService.updateJobInfo(jobInfo));
    }

    /**
     * 删除查询作业信息
     */
    @RequiresPermissions("jobinfo:info:remove")
    @Log(title = "查询作业信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(jobInfoService.deleteJobInfoByIds(ids));
    }

    //根据作业ID获取作业信息
    @GetMapping("/getHomeWorkInfo")
    @ResponseBody
    public JobInfo getHomeWorkInfo(Long id) {
        System.err.println("homeworkinfo:" + id);
        return (JobInfo) jobInfoService.selectJobInfoById(id);
    }

    /**
     * 查看作业详细
     */
    @RequiresPermissions("jobinfo:info:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("jobinfo", jobInfoService.selectJobInfoById(id));
        return prefix + "/detail";
    }

}











